#include "e_mod_main.h"
#ifdef HAVE_XMMS_XMMSCTRL_H
#include <xmms/xmmsctrl.h>

int xmms_test_running(Eloquence_Face *face);
int xmms_get_session(Eloquence_Face *face);
void xmms_control_jump(Eloquence_Face *face);

void xmms_init_menu(E_Menu *menu, Eloquence_Face *face){
  E_Menu_Item *mi;
  
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Play/Pause"));
  e_menu_item_callback_set(mi, xmms_control_play_pause, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Next Song"));
  e_menu_item_callback_set(mi, xmms_control_track_next, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Previous Song"));
  e_menu_item_callback_set(mi, xmms_control_track_prev, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Jump To..."));
  e_menu_item_callback_set(mi, xmms_control_jump, face);
}

int xmms_update_song(Eloquence_Face *face){
  int state = xmms_test_running(face);
  
  if(state == 1){
    int pos = xmms_remote_get_playlist_pos(face->session.id);
    face->song = xmms_remote_get_playlist_title(face->session.id, pos);

    draw_text(face);
  }
  if(state == 2){
    face->song = "XMMS has been paused";
    draw_text(face);
  }
  if(state == 3){
    face->song = "";
    draw_text(face);
  }
  if((state == 4) && (face->session.id != -1)){
    face->song = "";
    draw_text(face);
    face->recheck_timer = ecore_timer_add(SEARCH_REFRESH, xmms_get_session, face);
    face->session.id = -1;
  }
  return 1;
}

int xmms_test_running(Eloquence_Face *face){
  int state;

  if(xmms_remote_is_running(face->session.id)){
    if(xmms_remote_is_playing(face->session.id)){
      if(xmms_remote_is_paused(face->session.id)){
        return 2;
      }
      return 1;
    } 
    return 3;
  }
  return 4; 
}

int xmms_get_session(Eloquence_Face *face){

  if(face->session.id == -1)
    ecore_timer_del(face->recheck_timer);
      
  for(face->session.id = 0 ; face->session.id < 16 ; face->session.id++){
     if(xmms_remote_is_running(face->session.id)){
       return 0;
     }        
  }
  if(face->session.id != -1){
    face->session.id = 0;
  }
  return 0;
}

void xmms_control_play_pause(Eloquence_Face *face){
  xmms_remote_play_pause(face->session.id);
  xmms_update_song(face);
}

void xmms_control_track_next(Eloquence_Face *face){
  xmms_remote_playlist_next(face->session.id);
  xmms_update_song(face);
}

void xmms_control_track_prev(Eloquence_Face *face){
  xmms_remote_playlist_prev(face->session.id);
  xmms_update_song(face);
}

void xmms_control_vol_inc(Eloquence_Face *face){
  int volume = xmms_remote_get_main_volume(face->session.id);  

  int pos = xmms_remote_get_playlist_pos(face->session.id);
  face->song = xmms_remote_get_playlist_title(face->session.id, pos);
  sprintf(face->outbuf, "%s   --   Volume: %i%%", face->song, volume);
  face->song = face->outbuf;
  draw_text(face);
  
  xmms_remote_set_main_volume(face->session.id, volume+2);
}

void xmms_control_vol_dec(Eloquence_Face *face){
  int volume = xmms_remote_get_main_volume(face->session.id);

  int pos = xmms_remote_get_playlist_pos(face->session.id);
  face->song = xmms_remote_get_playlist_title(face->session.id, pos);
  sprintf(face->outbuf, "%s   --   Volume: %i%%", face->song, volume);
  face->song = face->outbuf;
  draw_text(face);
  
  xmms_remote_set_main_volume(face->session.id, volume-2);
}

void xmms_control_shuffle(Eloquence_Face *face){
  xmms_remote_toggle_shuffle(face->session.id);
}

void xmms_control_jump(Eloquence_Face *face){
	/*seems to work not everywhere */
//  xmms_remote_show_jump_box(face->session.id);
}

void xmms_add_file_callback(Eloquence_Face *face, 
		Evas_Object *obj, const char *emission, const char *source){
  /* Empty now, for drag'n'drop of files to play */
}
#endif
